#include "include/lyricsetting.h"
#include "ui_lyricsetting.h"
#include<QtGui>

LyricSetting::LyricSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LyricSetting)
{
    ui->setupUi(this);
    QSettings settings("HJF","Minip");
    if(settings.contains("DesktopLyricBackColorAlpha"))             //init color
    {
        backColor1.setAlpha(settings.value("DesktopLyricBackColorAlpha").toInt());
        backColor1.setRed(settings.value("DesktopLyricBackColorRed").toInt());
        backColor1.setGreen(settings.value("DesktopLyricBackColorGreen").toInt());
        backColor1.setBlue(settings.value("DesktopLyricBackColorBlue").toInt());
    }
    else
    {
        backColor1.setRgb(0,0,0);
        backColor1.setAlpha(0);
    }
    if(settings.contains("DesktopLyricColorAlpha"))             //init color
    {
        desktopLyricColor.setAlpha(settings.value("DesktopLyricColorAlpha").toInt());
        desktopLyricColor.setRed(settings.value("DesktopLyricColorRed").toInt());
        desktopLyricColor.setGreen(settings.value("DesktopLyricColorGreen").toInt());
        desktopLyricColor.setBlue(settings.value("DesktopLyricColorBlue").toInt());
    }
    else
    {
        desktopLyricColor.setRgb(123,211,89);
        desktopLyricColor.setAlpha(50);
    }
    if(settings.contains("DesktopLyricFontFamily"))
    {
        desktopLyricFont.setFamily(settings.value("DesktopLyricFontFamily").toString());
        desktopLyricFont.setPointSize(settings.value("DesktopLyricFontPointSize").toInt());
        desktopLyricFont.setWeight(settings.value("DesktopLyricFontWeight").toInt());
        desktopLyricFont.setItalic(settings.value("DesktopLyricFontItalic").toBool());
    }
    else
    {
        desktopLyricFont.setPointSize(50);
        desktopLyricFont.setWeight(50);
        desktopLyricFont.setFamily("sans-serif");
        desktopLyricFont.setItalic(false);
    }
    if(settings.contains("RollingLyricBackColorAlpha"))
    {
        backColor2.setAlpha(settings.value("RollingLyricBackColorAlpha").toInt());
        backColor2.setRed(settings.value("RollingLyricBackColorRed").toInt());
        backColor2.setGreen(settings.value("RollingLyricBackColorGreen").toInt());
        backColor2.setBlue(settings.value("RollingLyricBackColorBlue").toInt());
    }
    else
    {
        backColor2.setRgb(0,0,0);
        backColor2.setAlpha(0);
    }
    if(settings.contains("RollingLyricForeColorAlpha"))
    {
        foreColor.setAlpha(settings.value("RollingLyricForeColorAlpha").toInt());
        foreColor.setRed(settings.value("RollingLyricForeColorRed").toInt());
        foreColor.setGreen(settings.value("RollingLyricForeColorGreen").toInt());
        foreColor.setBlue(settings.value("RollingLyricForeColorBlue").toInt());
    }
    else
    {
        foreColor.setRgb(23,211,89);
        foreColor.setAlpha(50);
    }
    if(settings.contains("RollingLyricFontFamily"))
    {
        rollingLyricFont.setFamily(settings.value("RollingLyricFontFamily").toString());
        rollingLyricFont.setPointSize(settings.value("RollingLyricFontPointSize").toInt());
        rollingLyricFont.setWeight(settings.value("RollingLyricFontWeight").toInt());
        rollingLyricFont.setItalic(settings.value("RollingLyricFontItalic").toBool());
    }
    else
    {
        rollingLyricFont.setPointSize(50);
        rollingLyricFont.setWeight(50);
        rollingLyricFont.setFamily("sans-serif");
        rollingLyricFont.setItalic(false);
    }

    saveChanges();

    connect(ui->cancelBtn,SIGNAL(clicked()),this,SLOT(close()));
    connect(ui->okBtn,SIGNAL(clicked()),this,SLOT(saveChanges()));
    connect(ui->fontSetBtn1,SIGNAL(clicked()),this,SLOT(FontSetting1()));
    connect(ui->fontSetBtn2,SIGNAL(clicked()),this,SLOT(FontSetting2()));
    connect(ui->backColorSetBtn1,SIGNAL(clicked()),this,SLOT(BackColorsetting1()));
    connect(ui->colorSetBtn,SIGNAL(clicked()),this,SLOT(ColorSetting()));
    connect(ui->backColorSetBtn2,SIGNAL(clicked()),this,SLOT(BackColorsetting2()));
    connect(ui->foreColorSetBtn,SIGNAL(clicked()),this,SLOT(ForeColorSetting()));
}

LyricSetting::~LyricSetting()
{
    delete ui;
}

void LyricSetting::FontSetting1()
{
    bool ok;
    QFont f=QFontDialog::getFont(&ok,desktopLyricFont,this);
    if(ok)
        desktopLyricFont=f;
}

void LyricSetting::FontSetting2()
{
    bool ok;
    QFont f=QFontDialog::getFont(&ok,rollingLyricFont,this);
    if(ok)
        rollingLyricFont=f;
}

void LyricSetting::ColorSetting()
{
    QColor c=QColorDialog::getColor(desktopLyricColor,this);
    if(c.isValid())
        desktopLyricColor=c;
}

void LyricSetting::BackColorsetting1()
{
    QColor c=QColorDialog::getColor(backColor1,this);
    if(c.isValid())
        backColor1=c;
}

void LyricSetting::BackColorsetting2()
{
    QColor c=QColorDialog::getColor(backColor2,this);
    if(c.isValid())
        backColor2=c;
}

void LyricSetting::ForeColorSetting()
{
    QColor c=QColorDialog::getColor(foreColor,this);
    if(c.isValid())
        foreColor=c;
}

void LyricSetting::saveChanges()
{
    QSettings settings("HJF","Minip");

    settings.setValue("DesktopLyricBackColorAlpha",backColor1.alpha());
    settings.setValue("DesktopLyricBackColorRed",backColor1.red());
    settings.setValue("DesktopLyricBackColorGreen",backColor1.green());
    settings.setValue("DesktopLyricBackColorRed",backColor1.blue());

    settings.setValue("DesktopLyricColorAlpha",desktopLyricColor.alpha());
    settings.setValue("DesktopLyricColorRed",desktopLyricColor.red());
    settings.setValue("DesktopLyricColorGreen",desktopLyricColor.green());
    settings.setValue("DesktopLyricColorRed",desktopLyricColor.blue());

    settings.setValue("DesktopLyricFontFamily",desktopLyricFont.family());
    settings.setValue("DesktopLyricFontPointSize",desktopLyricFont.pointSize());
    settings.setValue("DesktopLyricFontWeight",desktopLyricFont.weight());
    settings.setValue("DesktopLyricFontItalic",desktopLyricFont.italic());

    settings.setValue("RollingLyricBackColorAlpha",backColor2.alpha());
    settings.setValue("RollingLyricBackColorRed",backColor2.red());
    settings.setValue("RollingLyricBackColorGreen",backColor2.green());
    settings.setValue("RollingLyricBackColorBlue",backColor2.blue());

    settings.setValue("RollingLyricForeColorAlpha",foreColor.alpha());
    settings.setValue("RollingLyricForeColorRed",foreColor.red());
    settings.setValue("RollingLyricForeColorGreen",foreColor.green());
    settings.setValue("RollingLyricForeColorBlue",foreColor.blue());

    settings.setValue("RollingLyricFontFamily",rollingLyricFont.family());
    settings.setValue("RollingLyricFontPointSize",rollingLyricFont.pointSize());
    settings.setValue("RollingLyricFontWeight",rollingLyricFont.weight());
    settings.setValue("RollingLyricFontItalic",rollingLyricFont.italic());
    settings.sync();
    close();
}

QFont LyricSetting::getDesktopLyricFont()
{
    return desktopLyricFont;
}

QFont LyricSetting::getRollingLyricFont()
{
    return rollingLyricFont;
}

QColor LyricSetting::getDesktopLyricBackColor()
{
    return backColor1;
}

QColor LyricSetting::getDesktopLyricColor()
{
    return desktopLyricColor;
}

QColor LyricSetting::getRollingLyricBackColor()
{
    return backColor2;
}

QColor LyricSetting::getRollingLyricForeColor()
{
    return foreColor;
}

void LyricSetting::closeEvent(QCloseEvent *ce)
{
    emit closeRequest();
    ce->accept();
}
